<?php
require './include/global.php';
require './include/validate.php';
require './include/all_left.php';

$job_id = $_GET[id]+0;
$jobDetail = getJobDetail($job_id);

$select_list = $_POST['select_list'];
$contact_list = $_POST['contact_list'];
if($select_list || $contact_list) {
	$sql = "SELECT re_uid FROM pa_contact WHERE `re_isapproved` = '1' AND `re_aliasid` = '".$job_id."'";
	$ori_friends = $DB->fetch_all($sql);
	if(!empty($ori_friends)){
		foreach($ori_friends as $v){
				if(!@in_array($v['re_uid'], $select_list) && areFriends($v[re_uid], $_SESSION[uid])) {
					$DB->query("UPDATE pa_contact SET `re_isapproved` = '0' WHERE `re_uid` ='".$v['re_uid']."' AND `re_aliasid` = '".$job_id."'");
				}
		}
	}

	if(!empty($select_list)){
		foreach($select_list as $v) {
			$info = $DB->fetch_one_array("SELECT * FROM pa_recuiter WHERE rc_id = '".$v."'");

			//check if this guy does exist
			$sql = "SELECT * FROM pa_contact WHERE re_uid = '".$v."' AND `re_aliasid` = '".$job_id."'";
			$contactArr = $DB->fetch_one_array($sql);
			$emailto = array();
			if(empty($contactArr)){
				$DB->query("INSERT INTO  pa_contact SET 
					`re_isapproved` = '1',
					`re_uid` = '".$v."',
					`re_aliasid` = '".$job_id."',
					`re_usertype` = 'recruiter',
					`re_uname` = '".$info[rc_uname]."',
					`re_fname` = '".$info[rc_fname]."',
					`re_lname` ='".$info[rc_lname]."',
					`re_email` ='".$info[rc_email]."',
					`re_address1` ='".$info[rc_address1]."',
					`re_address2`='" . $info[rc_address2]. "' , 
					`re_subject` = 'Manually added to this job',
					`re_regtime` = '".time()."', 
					`re_updtime` = '".time()."'
					");
				$emailto[$info['rc_email']] = $info['rc_copname'];

				//Send Email
				require_once("./include/class.phpmailer.php");

				if(!empty($emailto)){
					foreach($emailto as $email => $username){
						$mail=new PHPMailer();

						$mail->IsMail();
						$mail->From     = $_SESSION['useremail'];
						$mail->FromName = $_SESSION['realname'];
						$mail->AddAddress($email,$username);
						

						$mail->IsHTML(true);
						$mail->Subject = "Approved: ".$jobDetail['jo_title'].", ER Job Req#".$job_id;
						$mail->Body = "<p>You are receiving this message because the sender has approved for you to work on this job.  You now can submit candidates to this job.  To get started, login to your account at <a href='enetrecruiter.com'>enetRecruiter.com</a>  then go to view the Approved to Submit jobs section. </p> 
						<p><span style='color:gray;'>NOTE: This message was sent to you by a member of enetRecruiter.com. For questions regarding this job, please contact the sender.  Any questions regarding your account please contact us at <a href='mailto:account@enetrecruiter.com'>account@enetrecruiter.com</a>. </span></p> ";

						$mail->CharSet = 'UTF-8';
						$mail->Send();
					}
				}
			}else{
				$DB->query("UPDATE pa_contact SET
					`re_isapproved` = '1',
					`re_usertype` = 'recruiter',
					`re_uname` = '".$info[rc_uname]."',
					`re_fname` = '".$info[rc_fname]."',
					`re_lname` ='".$info[rc_lname]."',
					`re_email` ='".$info[rc_email]."',
					`re_address1` ='".$info[rc_address1]."',
					`re_address2`='" . $info[rc_address2]. "' , 
					`re_updtime` = '".time()."' WHERE 	
					`re_uid` = '".$v."' AND
					`re_aliasid` = '".$job_id."'
				");
			}
		}
	}
	echo "<meta http-equiv=\"refresh\" content=\"0;URL=myjobs.php\">";
	exit;
}

$sql = "SELECT * FROM pa_mycontacts WHERE myc_rcid = '".$_SESSION[uid]."' ORDER BY myc_created DESC";
$tp_contacts = $DB->fetch_all($sql);
foreach((array)$tp_contacts as $v) {
	if(areFriends($v[myc_rcid], $v[myc_fid])) {
		$pa_c = $DB->fetch_one_array("SELECT re_isapproved FROM pa_contact WHERE re_uid = '".$v[myc_fid]."' AND re_aliasid = '".$job_id."'");
		/*
		if($pa_c[re_isapproved] == 1) {
			$friends[] = $v;
		}else{
			$contacts[] = $v;
		}*/
		if($pa_c[re_isapproved] != 1) {
			$contacts[] = $v;
		}else{
			$friends[] = $v;
		}
	}
}

$smarty->display('header.tpl');


$smarty->display('allleft.tpl');

$smarty->assign('friends', $friends);
$smarty->assign('contacts', $contacts);
$smarty->display('addrecruiter.tpl');

$smarty->display('footer.tpl');
?>